<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/4/27
 * Time: 10:44
 */

namespace app\index\model;


use think\Model;
use app\index\model\GoodsNext;
use think\Exception;
use app\index\model\CrmCustomer;

class GoodsPhone extends Model
{
    /*
     * 白名单列表
     * */
    public function goodsPhone($output){
        try{
            $goodsPhone = self::alias('gp')->join('goods go','gp.course_id=go.id')->where('go.search_type','in',[1,2,3])->where('go.id','not in',[67,78])->field('gp.*,max(gp.id) as maxid')->group('gp.phone')->order('id desc')->select();
            $ids = array_column($goodsPhone, 'maxid');
            echo '-------------------开始执行----------------'.count($ids);
            foreach ($ids as $k=>$vid){
                $goods_phone = self::get($vid);
                $Customer = CrmCustomer::where('phone',$goods_phone->phone)->find();
                if(!$Customer) continue;
                if(!$Goods = Goods::where('id',$goods_phone->course_id)->find())continue;
                $GoodsNext = GoodsNext::where('goods_id',$goods_phone->course_id)->select();
                if(count($GoodsNext)>=2){
                    CrmCustomer::where('phone',$goods_phone->phone)->update(['public_yingqi'=>$goods_phone->course_id,'public_yingqi_course1'=>$GoodsNext[0]->id,'public_yingqi_course2'=>$GoodsNext[1]->id]);
                }
            }
            return count($ids);
        }catch (Exception $exception){
            return $exception->getMessage();
        }
    }

}